package com.musicapp;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonElement;

import java.util.ArrayList;
import java.util.List;

public class Floating extends View implements Music.ServiceCallbacks{

    private boolean isImage1 = true;
    private Music mMusic;
    private boolean mIsBound = false;
    List<ItemModel> itemList1 = FirstFragment.itemList1;
    List<ItemModel> itemList2 = FirstFragment.itemList2;
    List<ItemModel> itemList3 = FirstFragment.itemList3;
    List<ItemModel> itemList4 = FirstFragment.itemList4;
    List<SongModel> songList = FirstFragment.songList;
    private TextView textView1;
    private TextView textView2;
    private ImageView imageView1;
    private ImageView imageView2;
    private ImageView imageView3;
    private RelativeLayout rootLayout;
    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;


    public Floating(Context context) {
        super(context);
        init(context);
        playRandom();

//        展示列表
        imageView2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                com.musicapp.List dialog = new com.musicapp.List(getContext());
                dialog.show();
            }
        });

//        点击播放
//        Uri musicUri = Uri.parse(songList.get(0).getMusicurl());
        imageView1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isImage1){
                    if(Music.isPlaying==true){
                        stopMusic();
                    }
                    imageView1.setImageResource(R.drawable.zanting2);
//                    mMusic.play(musicUri,1);
                }else {
                    mMusic.pause();
                    imageView1.setImageResource(R.drawable.bofang2);
                }
                isImage1 = !isImage1 ;
            }
        });

        Intent intent = new Intent(getContext(), Music.class);
        getContext().bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);

    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Music.LocalBinder binder = (Music.LocalBinder) service;
            mMusic = binder.getService();
            mIsBound = true;
            binder.registerCallbacks(Floating.this);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mIsBound = false;
        }
    };

    private void init(Context context) {
        windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

//        根视图
        rootLayout = new RelativeLayout(context);
        rootLayout.setGravity(Gravity.CENTER_VERTICAL);
        rootLayout.setBackgroundColor(Color.GRAY);

//         TextView
        textView1 = new TextView(context);
        textView1.setText("歌名歌名");
        textView1.setTextSize(17);
        textView1.setTypeface(Typeface.DEFAULT_BOLD);
        RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        int marginLeft2 = getResources().getDimensionPixelSize(R.dimen.margin2);
        textParams1.setMargins(marginLeft2,0,0,0);
        textView1.setLayoutParams(textParams1);

        textView2 = new TextView(context);
        textView2.setText("歌手");
        textView2.setTextSize(17);
        RelativeLayout.LayoutParams textParams2 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
//        textParams2.addRule(RelativeLayout.RIGHT_OF,textView1.getId());
        int marginLeft3 = getResources().getDimensionPixelSize(R.dimen.margin3);
        textParams2.setMargins(marginLeft3,0,0,0);
        textView2.setLayoutParams(textParams2);

//        ImageView
        imageView1 = new ImageView(context);
        imageView1.setImageResource(R.drawable.bofang2);
        RelativeLayout.LayoutParams imageParams1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        int marginLeft4 = getResources().getDimensionPixelSize(R.dimen.margin4);
        imageParams1.setMargins(marginLeft4,10,0,0);
        imageView1.setLayoutParams(imageParams1);

        imageView2 = new ImageView(context);
        imageView2.setImageResource(R.drawable.liebiao2);
        RelativeLayout.LayoutParams imageParams2 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        int marginLeft5 = getResources().getDimensionPixelSize(R.dimen.margin5);
        imageParams2.setMargins(marginLeft5,0,0,10);
        imageView2.setLayoutParams(imageParams2);

        imageView3 = new ImageView(context);
        imageView3.setImageResource(R.drawable.liebiao2);
        RelativeLayout.LayoutParams imageParams3 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        int marginLeft = getResources().getDimensionPixelSize(R.dimen.margin1);
        imageParams3.setMargins(marginLeft,0,0,0);
        imageView3.setLayoutParams(imageParams3);

        rootLayout.addView(imageView3);
        rootLayout.addView(textView1);
        rootLayout.addView(textView2);
        rootLayout.addView(imageView1);
        rootLayout.addView(imageView2);

        // 设置悬浮视图的布局参数
        layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                230,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
//                        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
//                        | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR,
                PixelFormat.TRANSLUCENT);
        layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER;
    }

//    悬浮View出现、消失、销毁
    public void show() {
        windowManager.addView(rootLayout, layoutParams);
    }
    public void hide() {
        rootLayout.setVisibility(View.GONE);
    }
    public void reshow(){
        if(songList.size()!=0){

        }
        rootLayout.setVisibility(View.VISIBLE);
    }

//    随机添加模块
    public void playRandom(){
        int n = (int) (Math.random() * 4);
        List<ItemModel> List0 = new ArrayList<>();
        if (n == 1) {
            List0.addAll(itemList1);
        }else if (n == 2) {
            List0.addAll(itemList2);
        }else if (n == 3) {
            List0.addAll(itemList3);
        }else {
            List0.addAll(itemList4);
        }
//        Log.i("XXX",itemList1.get(1).getMusicName());
//        for (ItemModel ListElement : List0){
//            songList.add(ListElement.transModel());
//            Log.i("XXX",songList.get(i).getMusicname());
//            i++;
//        }
    }

    public void destroy() {
        windowManager.removeView(this);
        windowManager = null;
        layoutParams = null;
    }

    @Override
    public void onPlaybackStateChanged(boolean isPlaying) {
        if (!isPlaying) {
            imageView1.setImageResource(R.drawable.bofang);
        }
    }

    @Override
    public void onPlaybackProgressChanged(int currentPosition) {
    }

    public void stopMusic() {
        if (mIsBound && mMusic != null) {
            mMusic.stop();
        }
    }
}
